package com.mmall.util;

import com.mmall.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

/**
 * Created by root on 18-5-10.
 */
@Slf4j
public class RedisPoolUtil {

    public static String set(String key, String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getResource();
            result = jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{}, value:{} error", key, value, e);
            result = null;
            RedisPool.returnBrokenResource(jedis);
        }
        return result;
    }

    public static String get(String key) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getResource();
            result = jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{}, error", key, e);
            result = null;
            RedisPool.returnBrokenResource(jedis);
        }
        return result;
    }


    public static String setEx(String key, String value, int exTime) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getResource();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            log.error("get key:{}, error", key, e);
            result = null;
            RedisPool.returnBrokenResource(jedis);
        }
        return result;
    }

    public static Long expired(String key, int exTime) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getResource();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {
            log.error("get key:{}, error", key, e);
            result = null;
            RedisPool.returnBrokenResource(jedis);
        }
        return result;
    }

    public static Long del(String key) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getResource();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("get key:{}, error", key, e);
            result = null;
            RedisPool.returnBrokenResource(jedis);
        }
        return result;
    }
}
